package com.yuxi.config;





import com.yuxi.common.FileConfig;
import com.yuxi.common.token.JwtsUtil;
import com.yuxi.interceptor.LoginInterceptor;
import com.yuxi.interceptor.OptionsInterceptor;
import com.yuxi.interceptor.PhoneDeviceResolverHandlerInterceptor;
import com.yuxi.interceptor.RightsInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


import javax.annotation.Resource;
import java.io.File;

/**
 * @author sunke
 * @Date 2018/8/16 10:32
 * @description
 */
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {


/*    @Value("${file.upload.root}")
    private String filePath;*/


    @Autowired
    private FileConfig fileConfig;


    @Value("${debugRun}")
    private boolean debug;
    @Value("${rightsDebug}")
    private boolean rightsDebug;


    @Resource(name = "optionsInterceptor")
    private OptionsInterceptor optionsInterceptor;

    @Resource(name = "loginInterceptor")
    private LoginInterceptor loginInterceptor;

    @Resource(name = "rightsInterceptor")
    private RightsInterceptor rightsInterceptor;

    @Resource(name = "phoneDeviceResolverHandlerInterceptor")
    private PhoneDeviceResolverHandlerInterceptor phoneDeviceResolverHandlerInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/public/");

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/files/**").addResourceLocations("file:///" + fileConfig.getRoot() + File.separator);

    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {


        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("PUT", "DELETE", "GET", "POST", "OPTIONS")
                .allowedHeaders("X-Requested-with", "Content-Type", "authorization", "jsonType", "json-type", JwtsUtil.ADMIN_HEADER_KEY)
                .exposedHeaders("Authorization", "X-Requested-With", "Content-Type", "jsonType", "json-type", JwtsUtil.ADMIN_HEADER_KEY)
                .allowCredentials(false).maxAge(3600);

    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(optionsInterceptor).addPathPatterns("/**");

        registry.addInterceptor(phoneDeviceResolverHandlerInterceptor).addPathPatterns("/**/*.html");


        //"/sampleReport/getViewUrlPath", "/v1/**",
        if (!debug) {

            registry.addInterceptor(loginInterceptor).addPathPatterns("/manager/**")
                    .excludePathPatterns(

                            "/login", "/files/**", "/imageCode", "/html2Img", "/swagger-resources/**", "/doc.html", "/webjars/**");


            if (!rightsDebug) {

                //"/sampleReport/getViewUrlPath", "/v1/**",
                registry.addInterceptor(rightsInterceptor)
                        .addPathPatterns("/manager/**")
                        .excludePathPatterns(
                                "/**/exportExcelByCode/*",
                                "/companyIntegralLog/exportMonthExcelByCode/*",
                                "downloadFileByCode/**",
                                "/dataFilling/exportOneExcelByCode/*",
                                "/screen/**",
                                "/dataFilling/exportByCode/*",
                                "/code/*",
                                "/login", "/files/**", "/imageCode", "/html2Img", "/noRights/**", "/doc.html", "/swagger-resources/**", "/webjars/**");

            }


        }

    }


}
